package bupt.party.online.service.impl;

import java.util.ArrayList;
import java.util.List;

import bupt.party.online.dao.IBranchDao;
import bupt.party.online.model.Branch;
import bupt.party.online.service.IBranchService;

public class BranchServiceImpl implements IBranchService{
	private IBranchDao branchDao;
	
	public Branch findBranchById(int id) {
		//通过支部Id查找支部
		return branchDao.findBranchById(id);
	}

	public IBranchDao getBranchDao() {
		return branchDao;
	}

	public void setBranchDao(IBranchDao branchDao) {
		this.branchDao = branchDao;
	}

	public boolean addBranch(Branch branch) {
		// 增加支部
		String branchname=branch.getBranchName();
		branchDao.saveBranch(branch);
		return true;

	}

	public boolean deleteBranch(String branchname) {
		// 删除支部
		Branch branch = branchDao.queryBranchByBranchName(branchname);
		if(branch==null){
			return false;//删除失败
		}else{
			branchDao.deleteBranch(branch);
			return true;//删除成功
		}
	}
	
	public boolean updateBranch(Branch branch){
		//修改支部
		branchDao.updateBranch(branch);
		return true;//修改成功
	}
	public List<Branch> findAllBranch() {
		// 找到所有支部
		return branchDao.queryAll();
	}

	@Override
	public List<Branch> findChildBranchesById(int id) {
		return branchDao.findChildBranchesByID(id);
	}

	@Override
	public List<Branch> findParentBranches() {
		return this.branchDao.findParentBranchesByID();
	}

	@Override
	public String getBranchName(int id) {
		
		return this.branchDao.getBranchName(id);
	}
	
	public List<Branch> getNewsByCategory(int id, int pageNow,
			int pageSize) {
		// TODO Auto-generated method stub
		
		int firstResult = pageSize*(pageNow - 1);
		
		return branchDao.getResultsByPage(id, firstResult, pageSize);
		
	}
	
	public int getResultCountByTag(int id) {
		// TODO Auto-generated method stub

		if(id <= 0) return 0;
		
		else return this.branchDao.getResultCountByTag(id);
	}
	public int getResultCount(){
		return this.branchDao.getCount();
	}

	@Override
	public boolean deleteBranchById(int id) {
		Branch bean = this.branchDao.findBranchById(id);
		if(bean.getLevel()==3){
			this.branchDao.deleteBranch(bean);
		}else if(bean.getLevel()==2){
			List<Branch> children = this.branchDao.findChildBranchesByID(id);
			this.branchDao.deleteBranch(bean);
			if(children!=null){//有子孙支部才删除
				for(Branch c:children){
					this.branchDao.deleteBranch(c);
				}
			}	
		}
		return false;
	}

	@Override
	public Branch getParentBranchById(int id) {
		Branch branch = this.branchDao.findBranchById(id);
		if(branch==null){
			return null;
		}else if(branch.getId()==1){
			return null;
		}else{
			return this.branchDao.findBranchById(branch.getParentId());
		}
	}

	@Override
	public List<Branch> getBranchlevel3(int id) {
		Branch branch=this.branchDao.findBranchById(id);
		List<Branch> list=new ArrayList<Branch>();
		if(branch!=null){
			if(branch.getLevel()==3){
				list.add(branch);
			}else if(branch.getLevel()==2){
				list=this.branchDao.findChildBranchesByID(id);
			}else{
				List<Branch> listlevel2=this.branchDao.findChildBranchesByID(id);
				if(listlevel2!=null){
					for(Branch b:listlevel2){
						if(this.branchDao.findChildBranchesByID(b.getId())!=null){
							list.addAll(this.branchDao.findChildBranchesByID(b.getId()));
						}					
					}
				}
				
			}
		}
		return list;
	}

	@Override
	public boolean checkName(String name) {
		
		return this.branchDao.checkName(name);
	}

	public String getFrontTreeCodeById(Branch parent,List<Branch> branches) {
		// TODO Auto-generated method stub
		
		if(parent == null) return "";
		
		StringBuffer treeCode = new StringBuffer();
		
		
		List<Branch> childrenBranches = new ArrayList<Branch>();
		
		for(Branch b : branches){
			if(b.getParentId().equals(parent.getId())) childrenBranches.add(b);
		}
		
		if(childrenBranches.size() > 0){
			
			treeCode.append("<ul style=\"display:none;\">");
			
			for(Branch b: childrenBranches){
				
				if(childrenBranches.indexOf(b) == childrenBranches.size()-1){
					treeCode.append("<li class=\"last\">");
				}
				
				else treeCode.append("<li>");
				
				if(isParent(b,branches)) treeCode.append("<div class=\"hitarea expandable-hitarea\"></div>");
				
				treeCode.append("<a href=\"PartyNews_collegeNews?branchId=" +b.getId()+ "\">");
				
				treeCode.append(b.getBranchName());
				
				treeCode.append("</a>");
				treeCode.append(getFrontTreeCodeById(b,branches));
				treeCode.append("</li>");
			}
			
			treeCode.append("</ul>");
		}
		
		return treeCode.toString();
	}

	public String getBackTreeCodeById(Branch parent,List<Branch> branches) {
		// TODO Auto-generated method stub
		
		if(parent == null) return "";
		
		StringBuffer treeCode = new StringBuffer();
		
		
		List<Branch> childrenBranches = new ArrayList<Branch>();
		
		for(Branch b : branches){
			if(b.getParentId().equals(parent.getId())) childrenBranches.add(b);
		}
		
		if(childrenBranches.size() > 0){
			
			treeCode.append("<ul style=\"display:none;\">");
			
			for(Branch b: childrenBranches){
				
				if(childrenBranches.indexOf(b) == childrenBranches.size()-1){
					treeCode.append("<li class=\"last\">");
				}
				
				else treeCode.append("<li>");
				
				if(isParent(b,branches)) treeCode.append("<div class=\"hitarea expandable-hitarea\"></div>");
				
				treeCode.append("<a href=\"Admin_PartyNewsAction_manageCollegeNews?branchId="+ b.getId() +"\">");
				
				treeCode.append(b.getBranchName());
				
				treeCode.append("</a>");
				treeCode.append(getBackTreeCodeById(b,branches));
				treeCode.append("</li>");
			}
			
			treeCode.append("</ul>");
		}
		
		return treeCode.toString();
	}
	
	public boolean isParent(Branch node,List<Branch> allBranches){
		
		for(Branch b: allBranches){
			if(b.getParentId().equals(node.getId())) return true;
		}
		return false;
		
	}
	
	@Override
	public String getPageCode4BranchTree(boolean isFrontPage) {
		// TODO Auto-generated method stub
		
		List<Branch> level2Branches = this.branchDao.getLevel2Branches();
		
		StringBuffer treeCode = new StringBuffer();
		
		treeCode.append("<ul style=\"display:none;\">");
		
		for(Branch b : level2Branches)
		{
			treeCode.append("<li><div class=\"dot\"></div>");
			
			if(!isFrontPage) treeCode.append("<a href=\"Admin_PartyNewsAction_manageCollegeNews?branchId="+ b.getId() +"\">");
			
			else treeCode.append("<a href=\"PartyNews_collegeNews?branchId=" +b.getId()+ "\">");
			
			treeCode.append(b.getBranchName());
			
			treeCode.append("</a>");
			
			treeCode.append("</li>");
			
		}
		
		treeCode.append("</ul>");
		
		return treeCode.toString();
		
	}

	@Override
	public void getNodeChildrenBranches(Branch parentBranch,List<Branch> result) {
		// TODO Auto-generated method stub
	
		result.add(parentBranch);
		
		List<Branch> childrenBranches = findChildBranchesById(parentBranch.getId());
		
		if(childrenBranches == null || childrenBranches.size() <= 0) return;
		
		for(Branch b : childrenBranches){
			
			getNodeChildrenBranches(b,result);
			
		}
		
	}

	@Override
	public List<Branch> getBranchLevel2() {
		// TODO Auto-generated method stub
		return this.branchDao.getLevel2Branches();
	}

}